package com.example.aspect.achieve;

import com.example.constant.TokenConstant;
import com.example.pojo.entity.Admin;
import com.example.pojo.entity.User;
import com.example.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@Component
public class RedisListener extends KeyExpirationEventMessageListener {

    @Resource
    protected UserService userService;

    public RedisListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    @Override
    public void onMessage(Message key, byte[] pattern) {
        String string = key.toString();
        if (string.startsWith(TokenConstant.TOKEN_REDIS + "user:")) {
            String userName = string.replace(TokenConstant.TOKEN_REDIS + "user:", "");
            User user = new User();
            user.setUserName(userName);
            user.setIsLogin(0);
            userService.updateUser(user, false);
        }
        if (string.startsWith(TokenConstant.TOKEN_REDIS + "admin:")) {
            String userName = string.replace(TokenConstant.TOKEN_REDIS + "admin:", "");
            Admin admin = new Admin();
            admin.setUserName(userName);
        }
        System.out.println("RedisListener=>redis过期:" + string);
    }
}
